package org.example.mall.admin.service.manage.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.example.mall.admin.mapper.manage.SysLabelMapper;
import org.example.mall.admin.model.entity.manage.SysLabel;
import org.example.mall.admin.model.enums.manage.LabelTypeEnum;
import org.example.mall.admin.service.goods.SpecParamsService;
import org.example.mall.admin.service.manage.SysLabelService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统标签 服务实现类
 * </p>
 *
 * @author hzq
 * @since 2022-01-24
 */
@Service
@AllArgsConstructor
public class SysLabelServiceImpl extends ServiceImpl<SysLabelMapper, SysLabel> implements SysLabelService {

    private final SpecParamsService specParamsService;

    @Override
    public Boolean del(SysLabel label) {
        boolean remove = removeById(label.getId());
        specParamsService.delByGroupIds(Arrays.asList(label.getId()));
        return remove;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delGoodsGroup(Integer categoryId) {
        LambdaQueryWrapper<SysLabel> wrapper = Wrappers.<SysLabel>lambdaQuery()
                .eq(SysLabel::getLabelType, LabelTypeEnum.GOODS_GROUP.getType())
                .eq(SysLabel::getExtra, categoryId);
        // 删除属性列表
        specParamsService.delByGroupIds(list(wrapper).stream().map(SysLabel::getId).collect(Collectors.toList()));
        return remove(wrapper);
    }
}
